# *-* coding: utf-8 *-*
from __future__ import absolute_import

from PyQt4.QtGui import QMenu
from PyQt4.QtGui import QIcon
from PyQt4.QtGui import QStyle
from PyQt4.QtCore import QObject
from PyQt4.QtCore import SIGNAL
from PyQt4.QtCore import Qt
from PyQt4.QtCore import QCoreApplication

from ninja_ide import resources
from ninja_ide.core import settings


class MenuFile(QObject):

    def __init__(self, menuFile, toolbar, ide):
        QObject.__init__(self)

        newAction = menuFile.addAction(QIcon(resources.IMAGES['new']),
            self.tr("&New ($1+N)", settings.OS_KEY))
        newProjectAction = menuFile.addAction(
            QIcon(resources.IMAGES['newProj']),
            self.tr("New Pro&ject ($1+M)", settings.OS_KEY))
        menuFile.addSeparator()
        saveAction = menuFile.addAction(QIcon(resources.IMAGES['save']),
            self.tr("&Save ($1+S)", settings.OS_KEY))
        saveAsAction = menuFile.addAction(QIcon(resources.IMAGES['saveAs']),
            self.tr("Save &As"))
        saveAllAction = menuFile.addAction(QIcon(resources.IMAGES['saveAll']),
            self.tr("Save Pro&ject  ($1+Shift+S)", settings.OS_KEY))
        menuFile.addSeparator()
        reloadFileAction = menuFile.addAction(
            QIcon(resources.IMAGES['reload-file']),
            self.tr("Reload File (F5)"))
        menuFile.addSeparator()
        openAction = menuFile.addAction(QIcon(resources.IMAGES['open']),
            self.tr("&Open ($1+O)", settings.OS_KEY))
        openProjectAction = menuFile.addAction(
            QIcon(resources.IMAGES['openProj']),
            self.tr("Open &Project ($1+P)", settings.OS_KEY))
        openProjectTypeAction = menuFile.addAction(
            QIcon(resources.IMAGES['openProj']), self.tr("Open Project &Type"))
        menuFile.addSeparator()
        printFile = menuFile.addAction(QIcon(resources.IMAGES['print']),
            self.tr("Pr&int File ($1+I)", settings.OS_KEY))
        closeAction = menuFile.addAction(
            ide.style().standardIcon(QStyle.SP_DialogCloseButton),
            self.tr("&Close Tab ($1+W)", settings.OS_KEY))
        menuFile.addSeparator()
        exitAction = menuFile.addAction(
            ide.style().standardIcon(QStyle.SP_DialogCloseButton),
            self.tr("&Exit"))

        toolbar.addAction(newAction)
        toolbar.addAction(newProjectAction)
        toolbar.addAction(openAction)
        toolbar.addAction(openProjectAction)
        toolbar.addAction(saveAction)
        toolbar.addSeparator()

        self.connect(newAction, SIGNAL("triggered()"),
            ide._mainContainer.add_editor)
        self.connect(newProjectAction, SIGNAL("triggered()"),
            ide._explorer.create_new_project)
        self.connect(openAction, SIGNAL("triggered()"),
            ide._mainContainer.open_file)
        self.connect(saveAction, SIGNAL("triggered()"),
            ide._mainContainer.save_file)
        self.connect(saveAsAction, SIGNAL("triggered()"),
            ide._mainContainer.save_file_as)
        self.connect(saveAllAction, SIGNAL("triggered()"),
            ide._actions.save_project)
        self.connect(openProjectAction, SIGNAL("triggered()"),
            ide._explorer.open_project_folder)
#        QObject.connect(openProjectTypeAction, SIGNAL("triggered()"), self._open_project_type)
        self.connect(closeAction, SIGNAL("triggered()"),
            ide._mainContainer._actualTab.close_tab)
        self.connect(exitAction, SIGNAL("triggered()"),
            QCoreApplication.quit)
        QObject.connect(reloadFileAction, SIGNAL("triggered()"),
            ide._mainContainer.reload_file)
        self.connect(printFile, SIGNAL("triggered()"), ide._actions.print_file)

    def _open_project_type(self):
        self.openType = OpenProjectType(central_widget.CentralWidget())
        self.openType.show()


#class OpenProjectType(QDialog, ProjectWizard):
#
#    def __init__(self, main):
#        QDialog.__init__(self)
#        ProjectWizard.__init__(self)
#        self._main = main
#        self.setModal(True)
#        vbox = QVBoxLayout(self)
#        vbox.addWidget(QLabel(self.tr("Select the Type of Project:")))
#        self.listWidget = QListWidget()
#        projectTypes = self.types.keys()
#        projectTypes.sort()
#        self.listWidget.addItems(projectTypes)
#        vbox.addWidget(self.listWidget)
#        btnNext = QPushButton(self.tr("Next"))
#        vbox.addWidget(btnNext)
#        if len(projectTypes) > 0:
#            self.listWidget.setCurrentRow(0)
#        else:
#            btnNext.setEnabled(False)
#
#        self.connect(btnNext, SIGNAL("clicked()"), self._open_project)
#
#    def _open_project(self):
#        type_ = str(self.listWidget.currentItem().text())
#        extensions = self.types[type_].projectFiles()
#        if extensions is None:
#            self._main.open_project_folder()
#        else:
#            self._main.open_project_type(extensions)

